package com.song.day02定界符编码器.client;

import com.song.day01protobuf编码解码.PackageMessageProto;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class ClientHandler extends ChannelInboundHandlerAdapter {

//	public BlockingQueue<PackageMessage> queue = new LinkedBlockingQueue<PackageMessage>();
//	
//	
//	public PackageMessage readMsg() {
//		return this.queue.poll();
//	}
//	
//	public boolean isEmpQueue()
//	{
//		return CollectionUtils.isEmpty(queue);
//	}
	
	private int counter =0;
	
	static final String ECHO_REQ = "Hi , song welcome to netty .$_";
	
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		for(int i = 0 ;i<10 ;i++)
		{
			ctx.writeAndFlush(Unpooled.copiedBuffer(ECHO_REQ.getBytes()));
		}
	}

	public void channelRead(ChannelHandlerContext ctx, Object obj)
			throws Exception {
		System.out.println("this is " + ++counter+ " receive server msg is " + obj);
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
			throws Exception {
		// TODO Auto-generated method stub
		cause.printStackTrace();
		ctx.close();
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		ctx.flush();
//		ctx.close();
//		System.out.println("客户端接受数据结束");
	}

	

}
